/**
 * @desc 路由集合
 */
import express, { Request, Response, NextFunction } from 'express';
import { CGetIndexInfo } from '../controller';
import ErrorCode from '../utils/errorCode';
import userRouter from './user';
import utilsRouter from './utils';
import payListRouter from './payList';

// 获取错误路由
const { 
  Error404,
  Error500,
} = ErrorCode;

// 获取路由
const router = express.Router();

// 注册用户相关路由
router.use(userRouter);
// 注册用户工具路由
router.use(utilsRouter);
// 注册支付工具路由
router.use(payListRouter);

// 获取首页信息
router.get('/home/index', async (req: Request, res: Response, next) => {
  // 获取首页用户数据
  const result: any = await CGetIndexInfo() as any;
  res.json(result);
});

// 处理异常情况
router.use((req: Request, res: Response, next: NextFunction) => {
  res.status(404).json(Error404);
});

// 处理错误情况
router.use((err: { stack: any; }, req: Request, res: Response, next: NextFunction) => {
  res.status(500).json(Error500);
});

export default router;